/*
=============COPYRIGHT============ 
Tin Book - An I-Read-This prototype for Tin Can API
Copyright (C) 2012  Andrew Downes

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
<http://www.gnu.org/licenses/>.
*/



/*============CREATE LRS OBJECT==============*/
/* Hard coded for testing purposes only. */
var myTCVars = new Object();;
myTCVars.endpoint = "https://cloud.scorm.com/ScormEngineInterface/TCAPI/public/";
myTCVars.auth = 'Basic ' + Base64.encode("<account id>" + ':' + "<password>"); 
myTCVars.actor = '{ "mbox":["mailto:dummy@dummy.dummy2"], "name":["Bob2 Smith"] }';
/* END Hard coded for testing purposes only. */

tc_lrs = My_GetLRSObject(); //Comment out this line to leave tc_lrs as the result of TCDriver_GetLRSObject();

function My_GetLRSObject(){
	var lrsProps = ["endpoint","auth","actor","registration","activity_id", "grouping", "activity_platform"];
	var lrs = new Object();
	var prop;
	
	for (var i = 0; i<lrsProps.length; i++){
		prop = lrsProps[i];
		if (myTCVars[prop]){
			lrs[prop] = myTCVars[prop];
			delete myTCVars[prop];
		}
	}
	if(lrs.endpoint === undefined || lrs.endpoint == "" || lrs.auth === undefined || lrs.auth == ""){
		TCDriver_Log(lrs.endpoint + '//' + lrs.auth);
		return null;
	}
	
	lrs.extended = myTCVars;
	
	return lrs;
}

/*============END CREATE LRS OBJECT==============*/

/*============DOCUMENT READY==============*/
$(function(){
	$('#searchButton').click(function(){
		//TODO: sanitise the search input			
		$.getScript('https://www.googleapis.com/books/v1/volumes?q=' + $('#searchBox').val() + '&callback=listBooks');
		
	});

	$("#searchBox").keyup(function(event){
		if(event.keyCode == 13){
			$("#searchButton").click();
		}
	});

});
/*============END DOCUMENT READY==============*/

/*============SEARCH RESULTS==============*/
function listBooks(response) {
	$('#content').html('');
	
  for (var i = 0; i < response.items.length; i++) {
	var item = response.items[i];
	// in production code, item.text should have the HTML entities escaped.
	var bookElement = $('<div></div>'); 
	bookElement.append ($('<span class="IReadThisButton">I Read This!</span>'));
	bookElement.append ($('<span class="title">' + item.volumeInfo.title + '</span>'));
	bookElement.append ($('<span class="authors">Author(s): ' + item.volumeInfo.authors + '</span>'));
	bookElement.append ($('<span class="publishedDate">Published: ' + item.volumeInfo.publishedDate + '</span>'));		
	bookElement.append ($('<span class="publisher">Publisher: ' + item.volumeInfo.publisher + '</span>'));	
	bookElement.addClass('bookElement');			
	$('#content').append(bookElement);
	
				
  };
  
  
  $('.IReadThisButton').click(function(){
	/*============SEND STATEMENT==============*/
	var item = response.items[$('.bookElement').index($(this).parent('div'))]
	var stmt = 
		{ 
			"verb": "experienced",
			"object":{
				"id":item.volumeInfo.industryIdentifiers[0].identifier,
				"definition":{
					"name":{"en-US":item.volumeInfo.title, "en-GB":item.volumeInfo.title},
					"description":{"en-US":item.volumeInfo.description,"en-GB":item.volumeInfo.description}
				}
			}
		};
		
	TCDriver_Log(stmt);
	TCDriver_SendStatement(tc_lrs, stmt, function() {
		/*============DISPLAY SENT DATA==============*/
		var bookElement = $('<div></div>'); 
		bookElement.append ($('<span class="YouRead">You Read This:</span>'));
		bookElement.append ($('<span class="title">' + item.volumeInfo.title + '</span>'));
		bookElement.append ($('<span class="authors">Author(s): ' + item.volumeInfo.authors + '</span>'));
		bookElement.append ($('<span class="publishedDate">Published: ' + item.volumeInfo.publishedDate + '</span>'));		
		bookElement.append ($('<span class="publisher">Publisher: ' + item.volumeInfo.publisher + '</span>'));	
		bookElement.addClass('bookElement');			
		$('body').html(bookElement);
		$('body').append ($('<span class="instructions">Statement reported to LRS. Refresh this page to start again!</span>'));
		$('body').append ($('<p><a href="http://beta.projecttincan.com/ClientPrototypes/StatementViewer/" target="_blank">Click here to see the results</a></p>'));
	});
	/*============END DISPLAY SENT DATA==============*/
  });
  /*============END SEND STATEMENT==============*/
};
/*============END SEARCH RESULTS==============*/